/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.ui.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UIStep {
	private String packageName;
	private String className;
	private String text;
	private String[] fieldNames;
	private int[] fieldTypes;
	
	private static final Pattern p;
	
	static {
		p = Pattern.compile("<(.*)>");
	}
 	
	public UIStep(String packageName, String className, String text) {
		super();
		this.packageName = packageName;
		this.className = className;
		this.text = text;
	}
	
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
		String[] currentFieldNames = fieldNames;
		this.fieldNames = extractFieldNames(text);
		if (!Arrays.deepEquals(fieldNames,currentFieldNames)) {
			
		}
	}
	public void setFieldName(int index, String name) {
		fieldNames[index] = name;
	}
	public String[] getFieldNames() {
		return fieldNames;
	}
	public void setFieldType(int index, int type) {
		fieldTypes[index] = type;
	}
	public int getFieldType(int index) {
		return fieldTypes[index];
	}
	
	public static String[] extractFieldNames(String text) {
		ArrayList<String> list = new ArrayList<String>();
		Matcher m = p.matcher(text);
		while (m.find()) {
			String field = m.group();
			list.add(field);
		}
		return list.toArray(new String[0]);
	}
}
